package com.txunda.zhuohired.aty.user.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.home.CompanyBean;
import com.txunda.zhuohired.fgt.user.home.CompanyFgt;
import com.txunda.zhuohired.fgt.user.home.CompanyOneFgt;
import com.txunda.zhuohired.fgt.user.home.CompanyTwoFgt;
import com.txunda.zhuohired.http.pst.MemberHomePst;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/5 10:56
 * 功能描述：公司详情
 * 联系方式：jiahuanchen@126.com
 */

public class CompanyDetailsAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;

    @ViewInject(R.id.tl_company_details)
    private TabLayout tl_company_details;
    /**
     * 公司logo
     */
    @ViewInject(R.id.iv_company_details)
    private ImageView iv_company_details;
    /**
     * 公司名称
     */
    @ViewInject(R.id.tv_company_details_company)
    private TextView tv_company_details_company;
    /**
     * 行业
     */
    @ViewInject(R.id.tv_company_details_trade)
    private TextView tv_company_details_trade;
    /**
     * 规模
     */
    @ViewInject(R.id.tv_company_details_person)
    private TextView tv_company_details_person;

    @ViewInject(R.id.iv_company_auth)
    private ImageView iv_company_auth;

    @ViewInject(R.id.vp_company_details)
    private ViewPager vp_company_details;
    private ArrayList<Fragment> mFragments;
    private MyPagerAdapter myPagerAdapter;

    private List<String> tittleList;
    private String[] tittleStr = {"公司介绍", "全部职位", "兼职职位"};
    private String u_id = "";//公司id
    private MemberHomePst memberHomePst;
    private String token = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("公司详情");
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_company_details;
    }

    @Override
    protected void initialized() {
        memberHomePst = new MemberHomePst(this);
        u_id = getIntent().getStringExtra("u_id");
        token = application.getUserInfo().get("data");
        tittleList = new ArrayList();
        mFragments = new ArrayList<>();
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        for (int i = 0; i < tittleStr.length; i++) {
            tittleList.add(tittleStr[i]);
        }
        mFragments.add(CompanyFgt.getFgt(1, u_id));
        mFragments.add(CompanyOneFgt.getFgt(2, u_id));
        mFragments.add(CompanyTwoFgt.getFgt(3, u_id));
        //        for (int y = 0; y < tittleList.size(); y++) {
        //
        //        }
    }

    private void initHttp() {
        memberHomePst.companyInfo(u_id, "1", token);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("companyInfo")) {
            CompanyBean companyBean = GsonUtil.GsonToBean(jsonStr, CompanyBean.class);
            Glide.with(this).load(companyBean.getData().getBanner_data().getLogo()).error(R.mipmap.icon_defult)
                    .placeholder(R.mipmap.icon_defult).dontAnimate().into(iv_company_details);
            tv_company_details_company.setText(companyBean.getData().getBanner_data().getName());
            if (TextUtils.isEmpty(companyBean.getData().getBanner_data().getCompany_trade())) {
                tv_company_details_trade.setVisibility(View.GONE);
            } else {
                tv_company_details_trade.setVisibility(View.VISIBLE);
                tv_company_details_trade.setText("行业：" + companyBean.getData().getBanner_data().getCompany_scale());
            }
            if (TextUtils.isEmpty(companyBean.getData().getBanner_data().getCompany_scale())) {
                tv_company_details_person.setVisibility(View.GONE);
            } else {
                tv_company_details_person.setVisibility(View.VISIBLE);
                tv_company_details_person.setText("规模：" + companyBean.getData().getBanner_data().getCompany_scale());
            }
            if ("1".equals(companyBean.getData().getBanner_data().getAuth_status())) {
                iv_company_auth.setVisibility(View.VISIBLE);
            } else {
                iv_company_auth.setVisibility(View.GONE);
            }
            return;
        }
    }

    @Override
    protected void requestData() {
        initHttp();
        vp_company_details.setAdapter(myPagerAdapter);
        tl_company_details.setupWithViewPager(vp_company_details);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tittleList.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
